package com.yuan.controller;


import com.yuan.bean.LibraryCard;
import com.yuan.bean.UserLogin;
import com.yuan.service.LibraryCardService;
import com.yuan.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class DeskController {
    @Autowired
    UserLoginService userLoginService;

    @Autowired
    LibraryCardService libraryCardService;

    @PostMapping("/update")
    public String updateUser(@RequestParam("id")String id,@RequestParam("userName")String userName, @RequestParam("password")String password,
                             @RequestParam("company")String company, @RequestParam("job")String job, Model model){
        boolean b = userLoginService.updateUser(id, userName, password, company, job);
        model.addAttribute("msg",b);
        return "redirect:Login.html";
    }

    @GetMapping("/desk")
    public String desk(){
        return "desk";
    }

    //查询出用户到期的书籍
    @ResponseBody
    @GetMapping("/selectTime")
    public List<LibraryCard> outTime(HttpSession session){
        final UserLogin user =(UserLogin) session.getAttribute("User");
        Integer userId=user.getUserId();
        final List<LibraryCard> libraryCards = libraryCardService.outTime(userId);
        return libraryCards;
    }


}
